package com.itheima.wemedia.controller;

import com.itheima.admin.pojo.AdChannel;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.WmNewsAuthDto;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.pojo.WmUser;
import com.itheima.wemedia.service.ApiService;
import com.itheima.wemedia.service.WmMaterialService;
import com.itheima.wemedia.service.WmNewsService;
import com.itheima.wemedia.service.WmUserService;
import com.itheima.wemedia.vo.WmNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @description 所有对外提供远程调用方法
 * @package com.itheima.wemedia.controller
 */
@RestController
public class ApiController {

    @Autowired
    private WmUserService wmUserService;

    @Autowired
    private WmMaterialService wmMaterialService;

    @Autowired
    private ApiService apiService;

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 通过app端用户id查询自媒体人信息
     * @param apUserId
     * @return
     */
    @GetMapping("/api/wmUser/getByApUserId/{apUserId}")
    public ResultVo<WmUser> getByApUserId(@PathVariable(value = "apUserId") Long apUserId){
        WmUser wmUser = wmUserService.getByApUserId(apUserId);
        return ResultVo.ok(wmUser);
    }

    /**
     * 添加自媒体账号
     */
    @PostMapping("/api/wmUser/add")
    public ResultVo<WmUser> add(@RequestBody WmUser wmUser){
        // mybatis plus插入数据时，会自动生成id, 且设置实体的id属性中
        if (wmUserService.save(wmUser)) {
            // wmUser中的id属性就有值
            return ResultVo.ok(wmUser);
        }
        return ResultVo.error();
    }

    /**
     * 添加素材
     * @param wmMaterial
     * @return
     */
    @PostMapping("/api/wmMaterial/add")
    public ResultVo add(@RequestBody WmMaterial wmMaterial){
        if(wmMaterialService.save(wmMaterial)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 查询所有频道列表
     * @return
     */
    @GetMapping("/channel/channels")
    public ResultVo channelList(){
        List<AdChannel> list = apiService.getChannelList();
        return ResultVo.ok(list);
    }

    /**
     * 通过id查询文章信息
     * @param wmNewsId
     * @return
     */
    @GetMapping("/api/wmNews/{wmNewsId}")
    public ResultVo<WmNews> getById(@PathVariable(name = "wmNewsId") Long wmNewsId){
        return ResultVo.ok(wmNewsService.getById(wmNewsId));
    }

    /**
     * 更新文章信息
     * @param wmNews
     * @return
     */
    @PutMapping("/api/wmNews/update")
    public ResultVo<WmNews> update(@RequestBody WmNews wmNews){
        if (wmNewsService.updateById(wmNews)) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 给平台管理系统，查询文章列表，人工审核用的
     * @return
     */
    @PostMapping("/news/list_vo")
    public PageResultVo list4Admin(@RequestBody WmNewsPageReqDto dto){
        PageResultVo vo = wmNewsService.pagelist4Admin(dto);
        return vo;
    }

    /**
     * 给平台管理系统，人工审核 通过
     * @return
     */
    @PostMapping("/news/auth_pass")
    public ResultVo authPass(@RequestBody WmNewsAuthDto dto){
       if (wmNewsService.authPass(dto)){
           return ResultVo.ok();
       } else {
           return ResultVo.error();
       }
    }

    /**
     * 给平台管理系统，人工审核 驳回
     * @return
     */
    @PostMapping("/news/auth_fail")
    public ResultVo authFail(@RequestBody WmNewsAuthDto dto){
        if (wmNewsService.authFail(dto)){
            return ResultVo.ok();
        } else {
            return ResultVo.error();
        }
    }

    /**
     * 查询文章详情
     * @param wmNewsId
     * @return
     */
    @GetMapping("/news/one_vo/{wmNewsId}")
    public ResultVo getOne(@PathVariable(value = "wmNewsId") Long wmNewsId){
        WmNewsVo vo = wmNewsService.getOneVo(wmNewsId);
        return ResultVo.ok(vo);
    }

}
